import { Component, OnInit } from '@angular/core';
import { NzMessageService } from 'ng-zorro-antd/message';
import { Router } from '@angular/router';
import { makeupDescentArrs } from '../recyc-market-manage.pipe';
import { PermissionService } from '../../../../service/permission.service';
import { LovemakeupDescentService } from '../services/love-makeup-descent.service';

@Component({
  selector: 'app-love-makeup-promotion',
  templateUrl: './love-makeup-promotion.component.html',
  styleUrls: ['./love-makeup-promotion.component.scss']
})
export class LoveMakeupPromotionComponent implements OnInit {
  // 路由缓存恢复时
  static updateCache: any = null;

  statusOptions = [
    {value: 1, label: '未开始'},
    {value: 2, label: '进行中'},
    {value: 3, label: '已结束'},
    {value: 4, label: '已停止'},
    {value: 5, label: '草稿'}
  ];
  userTypeOptions = [
    {value: 0, label: '通用'},
    {value: 1, label: 'C端'},
    {value: 2, label: 'B端'}
  ];
  applyColourOptions = [
    {value: 1, label: '二手'},
    {value: 2, label: '准新'}
  ];
  searchOptions = [
    { type: 1, labelText: '前端名称', valueKey: 'showCopy', value: '' },
    { type: 1, labelText: '备注', valueKey: 'name', value: '' },
    {
      type: 2,
      labelText: '适用用户',
      valueKey: 'userType',
      value: '',
      selectOptions: [...this.userTypeOptions],
    },
    {
      type: 2,
      labelText: '适用成色',
      valueKey: 'applyColour',
      value: '',
      selectOptions: [...this.applyColourOptions],
    },
    {
      type: 2,
      labelText: '促销状态',
      valueKey: 'status',
      value: '',
      selectOptions: [...this.statusOptions],
    },
    { type: 3, labelText: '促销时间', valueKey: ['beginTime', 'endTime'], value: [], isTimeSelect: true },
  ];
  searchParams: any = {};

  tableParams: any = {
    loading: false,
    tableData: [],
    page: 1,
    pageSize: 20,
    total: 0
  }

  constructor(
    private router: Router,
    private message: NzMessageService,
    public permission: PermissionService,
    private services: LovemakeupDescentService
  ) { }

  ngOnInit(): void {
    LoveMakeupPromotionComponent.updateCache = () => {
      this.loadTable();
    }
  }

  async loadTable() {
    let params = {
      ...this.searchParams,
      page: this.tableParams.page,
      pageSize: this.tableParams.pageSize
    }
    this.tableParams.loading = true;
    const res: any = await this.services.getdescentList(params);
    this.tableParams.loading = false;

    if (res.code === 0) {
      const { records, total } = res.data || {};
      this.tableParams.tableData = records || [];
      this.tableParams.total = total || 0;
    } else {
      this.message.error(res.message);
    }
  }

  queryData(params: any) {
    this.searchParams = params;
    this.tableParams.page = 1;
    this.loadTable();
  }

  resetData(params: any) {
    this.searchParams = params;
    this.tableParams.page = 1;
    this.loadTable();
  }

  // PageIndex Change
  onPageIndexChange(index: number) {
    this.tableParams.page = index;
    this.loadTable();
  }
  
  // PageSize Change
  onPageSizeChange(sizeNum: number) {
    this.tableParams.page = 1;
    this.tableParams.pageSize = sizeNum;
    this.loadTable();
  }

  /**
   * 页面跳转
   * @param type 0：新增 1：编辑 2：查看 3：复制
   * @param item 
   */
  jumpPage(type: number, item?: any) {
    let url = type === 2 ? '/aisi-recyc/loveMakeupPromotionDetails' : '/aisi-recyc/loveMakeupPromotionEdit';
    let obj: any = {
      type
    };
    if ([1, 2, 3].includes(type)) {
      obj.id = item.id
    }
    this.router.navigate([url],{
      queryParams: obj
    })
  }

  /**
   * 停止活动 | 删除活动
   * @param item 
   * @param status 4：停止 8：删除
   */
  async stopDelActivity(item: any, status: number) {
    let params = {
      id: item.id,
      status: status
    }
    const { code, message }: any = await this.services.updateDescentItem(params);
    if (code === 0) {
      this.message.success((status === 4 ? '停止' : '删除') + '活动成功');
      this.loadTable();
    } else {
      this.message.error(message || (status === 4 ? '停止' : '删除') + '活动失败');
    }
  }

  modelToText(str: string) {
    if (str == '0') {
      return '全部';
    } else if (str) {
      return str.split(',').length;
    } else {
      return '-';
    }
  }
}
